package com.googlecode.cswish.struts;

import java.io.IOException;
import java.io.Writer;

import com.opensymphony.sitemesh.Content;


public class SiteMeshContentWrapper implements Content {

	private Content content;
	
	private String header;
	private String footer;
	
	public SiteMeshContentWrapper(Content content, String header, String footer) {
		this.content = content;
		this.header = header;
		this.footer = footer;
	}

	public void writeOriginal(Writer writer) throws IOException {
		if (header != null && header.length() > 0) {
			writer.write(header);
		}
		content.writeOriginal(writer);
		if (footer != null && footer.length() > 0) {
			writer.write(footer);
		}
	}

	public int originalLength() {
		return content.originalLength() 
				+ (header == null ? 0 : header.length()) + (footer == null ? 0 : footer.length());
	}

	public void writeBody(Writer out) throws IOException {
		content.writeBody(out);
		if (footer != null && footer.length() > 0) {
			out.write(footer);
		}
	}

	public void writeHead(Writer out) throws IOException {
		content.writeHead(out);
		if (header != null && header.length() > 0) {
			out.write(header);
		}
	}

	public String getTitle() {
		return content.getTitle();
	}

	public String getProperty(String name) {
		return content.getProperty(name);
	}

	public String[] getPropertyKeys() {
		return content.getPropertyKeys();
	}

	public void addProperty(String name, String value) {
		content.addProperty(name, value);
	}
}
